<?php
namespace app\water\controller;

use app\common\controller\Smscm;
use app\common\model\Users;
use app\common\model\Users as UserM;
use think\Validate;

class Login extends Auth
{
    public $noNeedLogin = ['*'];

    public function index()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $rule = [
                'phone|手机号' => 'require|regex:^1\d{10}',
                //'code|验证码' => 'require|number',
                'type|类型' => 'require|number',
            ];
            // 定义验证提示
            $message = [
                //'code.require' => '验证码不能为空',
                //'code.number' => '验证码必须为数字',
                'phone.regex' => '手机号不正确',
                'phone.require' => '请输入手机号',
                'type.require' => '请输入登录方式',
                'type.number' => '登录方式格式不正确',
            ];
            $validate = new Validate($rule, $message);
            if (!$validate->check($data)) {
                $this->result($validate->getError(), '', 204);
            }
            $user = UserM::where('phone', $data['phone'])
                ->where('role', '1')
                ->find();
            //短信登录
            if ($data['type'] == '1') {
                if (empty($user)) {
                    $this->result('当前手机号未注册', '', 204);
                }
                if ($user['status'] == 2) {
                    $this->result('当前账号正在审核中', '', 204);
                } else if ($user['status'] == 1) {
                    $this->result('当前手机号暂时无法登录', '', 204);
                }
                //if ($data['code'] != '123456') {
                //    $Smscm = new Smscm();
                //    $res = $Smscm->check_smscode($data['phone'], $data['code'], 2);
                //    if ($res['code'] == 204) $this->result($res['msg'], '', 204);
                //}
                $token = token_rand('32', '1');
                $logininfo = array(
                    'waiter_token' => $token,
                );
                $re = UserM::where('id', $user['id'])->update($logininfo);
                $row['token'] = $token;
                if ($re) {
                    $this->result('登录成功', $row, 200);
                } else {
                    $this->result('系统繁忙，请稍后', '', 204);
                }
            }
            //账号密码登录
            if ($data['type'] == '2') {
                $userModel = new Users();
                $code = $userModel->checkUser($data);
                switch ($code) {
                    case 2000;
                        $token = token_rand();
                        Users::where(['phone' => $data['phone']])->update(['waiter_token' => $token]);
                        $userInfo = Users::where('u.phone', $data['phone'])
                            ->field('u.id,u.phone,u.real_name,u.waiter_token as login_token,u.avatar,s.name')
                            ->alias('u')
                            ->leftJoin('qt_store s', 'u.store_id = s.id')
                            ->find();
                        $userInfo['avatar'] = get_img_path($userInfo['avatar']);
                        $this->result('登录成功', $userInfo, 200);
                        break;
                    case 10001;
                        $this->result('用户不存在，请先去注册', '', 205);
                        break;
                    case 10002;
                        $this->result('账户被禁用，请联系客服', '', 206);
                        break;
                    case 10003;
                        $this->result('账号密码错误', '', 207);
                        break;
                }
            }
            $this->result('无效的参数', '', 204);
        } else {
            $this->result('请使用post请求', '', 204);
        }
    }

    /**
     * 获取用户信息
     */
    public function userInfo()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data['token'])) {
                $this->result('缺少参数', '', 204);
            }
            $userInfo = Users::where('u.login_token', $data['token'])
                ->field('u.phone,u.real_name,u.login_token,u.avatar,s.name')
                ->alias('u')
                ->leftJoin('qt_store s', 'u.store_id = s.id')
                ->find();
            $userInfo['avatar'] = get_img_path($userInfo['avatar']);
            $this->result('登录成功', $userInfo, 200);
        } else {
            $this->result('请使用post请求', '', 204);
        }
    }

}
